package main

import (
	"fmt"
	"strconv"
	"sync"
	"time"
)

func test4main() {
	ch := make(chan string, 20)
	ch1 := make(chan string, 20)
	ch3 := make(chan struct{}, 20)
	var wg sync.WaitGroup

	wg.Add(2)
	go func() {
		go chanenter(ch)
		wg.Done()
	}()

	go func() {
		go chanenter(ch)
		wg.Done()
	}()

	// 等待所有 chanenter 的 goroutine 完成
	wg.Wait()
	go changoout(ch)

	chanMultipF3(ch, ch1, ch3)
	// 可以选择在此处关闭通道（例如，如果不再发送数据）
}

func chanenter(ch chan<- string) {
	for i := 0; i < 5; i++ {
		ch <- strconv.Itoa(i)
		time.Sleep(1 * time.Second)
		fmt.Println("enter : ", strconv.Itoa(i))
	}
}
func changoout(ch <-chan string) {
	for a := range ch {
		fmt.Println("go outgo outgo out: ", a)
	}
}
func changooutend(ch <-chan string) {
	for a := range ch {
		fmt.Println("changooutendchangooutend: ", a)
	}
}
func chanMultipF3(ch <-chan string, ch1 <-chan string, ch2 <-chan struct{}) {
	i := 0
	for {
		select {
		case a, ok := <-ch1:
			if !ok {
				fmt.Println("chanMultipF3 ch1 Error : ", a)
			}
			fmt.Println("chanMultipF3 ch1 : ", a)
		case a, ok := <-ch2:
			if !ok {
				fmt.Println("chanMultipF3 ch2 Error : ", a)
			}
			fmt.Println("chanMultipF3 ch2 : ", a)
		case a, ok := <-ch:
			if !ok {
				fmt.Println("chanMultipF3 ch Error : ", a)
			}
			fmt.Println("chanMultipF3 ch : ", a)
		default:
			// 如果所有通道都没有数据可供读取，default 分支帮助避免阻塞
			fmt.Println("No channel ready")
		}
		i++
	}
}
